package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.CategoryDao;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.common.CustomException;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> wapper1 = new LambdaQueryWrapper<>();
        wapper1.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(wapper1);

        if(count1>0){
            throw new CustomException("该菜品以关联,无法删除");
        }
        LambdaQueryWrapper<Setmeal> wapper2 = new LambdaQueryWrapper<>();
        wapper2.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(wapper2);

        if(count2>0){
            throw new CustomException("该套餐以关联,无法删除");
        }
        super.removeById(id);
    }
}
